package indi.kider.server.employee.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.employee.AppUser;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Enterprise;
import indi.kider.server.employee.model.PwdChanger;
import indi.kider.server.employee.service.EmployeeService;
import indi.kider.server.employee.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by shixj on 2018/1/11.
 */
@RestController
@RequestMapping(value = "employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private AuthcService authcService;

    /**
     * @api {GET} /current
     * @apiGroup employee
     * @apiDescription 获取登录用户最新信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee/current
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"","title":"http://ovfozo1f4.bkt.clouddn.com/1.png","state":null,"enterpriseId":null,"roles":"1","lastLoginTime":"2018-04-25 17:09:59","loginIp":null,"loginName":"shixj"}}
     */
    @GetMapping(value = "/current")
    public CommonResponse getCurrentUser(HttpServletRequest request) throws Exception {
        Employee employee = authcService.getCurrentUser().getData();
        employee.setPwd(null);
        return CommonResponse.buildGetSuccessResponse(employee);
    }

    /**
     * @api {POST} /list
     * @apiGroup employee
     * @apiDescription 根据字段查询获取用户信息列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee/list
     * {"num":"0","size":"8","query":{"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"","title":"http://ovfozo1f4.bkt.clouddn.com/1.png","state":null,"enterpriseId":null,"roles":"1","lastLoginTime":"2018-04-25 15:16:36","loginIp":null,"loginName":"shixj"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"123456","title":"http://ovfozo1f4.bkt.clouddn.com/1.png","state":null,"enterpriseId":null,"roles":"1","lastLoginTime":"2018-04-25 16:43:10","loginIp":null,"loginName":"shixj"}],"total":1,"size":0}}
     */
    @PostMapping("/list")
    public CommonResponse getEmployees(@RequestBody PageRequest<Employee> employeePageRequest) {
        Employee employee = authcService.getCurrentUser().getData();
        if (!employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//非超级管理员
            employeePageRequest.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        return CommonResponse.buildGetSuccessResponse(employeeService.getEmployees(employeePageRequest));
    }


    /**
     * 不分页
     * @param enterprise
     * http://localhost:9002/human-resources/employee/getEmployeesNoPage
     * @return
     */
    @PostMapping("/getEmployeesNoPage")
    public CommonResponse getEmployeesNoPage(@RequestBody Enterprise enterprise) {
        return CommonResponse.buildGetSuccessResponse(employeeService.getEmployeesNoPage(enterprise.getId()));
    }

    /**
     * 查询合作用户
     * @param employeePageRequest
     * http://localhost:9002/human-resources/employee/hzlist
     * @return
     */
    @PostMapping("/hzlist")
    public CommonResponse getHzEmployees(@RequestBody PageRequest<Employee> employeePageRequest) {
        Employee employee = authcService.getCurrentUser().getData();
        return CommonResponse.buildGetSuccessResponse(employeeService.getHzEmployees(employeePageRequest,employee));

    }

    /**
     * @api {GET} /{id}
     * @apiGroup employee
     * @apiDescription 根据id获取用户信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"123456","title":"http://ovfozo1f4.bkt.clouddn.com/1.png","state":null,"enterpriseId":null,"roles":"1","lastLoginTime":"2018-04-25 16:13:55","loginIp":null,"loginName":"shixj"}}
     */
    @GetMapping("{employeeId}")
    public CommonResponse<Employee> getEmployeeById(@PathVariable Long employeeId) {
        return CommonResponse.buildGetSuccessResponse(employeeService.getEmployeeById(employeeId));
    }

    /**
     * @api {GET} loginName/{loginName}
     * @apiGroup employee
     * @apiDescription 根据登录名或手机号码获取用户
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee/loginName/shixj
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"123456","title":null,"state":null,"enterpriseId":null,"roles":null,"lastLoginTime":null,"loginIp":null,"loginName":"shixj"}]}
     */
    @GetMapping("/loginName/{loginName}")
    public CommonResponse<List<Employee>> getEmploeesToLogin(@PathVariable String loginName) {
        return CommonResponse.buildGetSuccessResponse(employeeService.getUsersToLogin(loginName));
    }

    /**
     * @api {POST} loginName/fuzzy/{loginName}
     * @apiGroup employee
     * @apiDescription 根据登录名或手机号码获取用户(模糊查询)
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee/loginName/fuzzy
     * {"loginName":"shi"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"123456","title":null,"state":null,"enterpriseId":null,"roles":null,"lastLoginTime":null,"loginIp":null,"loginName":"shixj"}]}
     */
    @PostMapping("/loginName/fuzzy")
    public CommonResponse<List<Employee>> getUsersByLoginNameWithLike(@RequestBody Employee employee) {
        return CommonResponse.buildGetSuccessResponse(employeeService.getUsersByLoginNameWithLike(employee.getLoginName()));
    }

    /**
     * @api {POST} /
     * @apiGroup employee
     * @apiDescription 新增雇员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee
     * {"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"123456","title":null,"state":null,"enterpriseId":null,"roles":null,"lastLoginTime":null,"loginIp":null,"loginName":"shixj"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"","title":null,"state":null,"enterpriseId":null,"roles":null,"lastLoginTime":null,"loginIp":null,"loginName":"shixj"}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody Employee employee, HttpServletRequest request) {
        try {
            if (employee.getLoginName() != null)
                return CommonResponse.buildAddSuccessResponse(employeeService.add(employee));
            else return CommonResponse.buildBadRequestResponse("登录名不能为空！");
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry"))
                return CommonResponse.buildBadRequestResponse("当前登录名已存在！");
            return CommonResponse.buildBadRequestResponse(e.getMessage());
        }

    }


    /**
     * @api {PUT} /
     * @apiGroup employee
     * @apiDescription 修改雇员信息（密码无法修改）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee
     * {"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"123456","title":null,"state":null,"enterpriseId":null,"roles":null,"lastLoginTime":null,"loginIp":null,"loginName":"shixj"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改成功！","data":{"id":1,"name":"史献建","phone":"18013897594","verification":"sss","email":"sss","cardId":"ss","cardimgBefore":"sss","cardimgback":"sss","position":"ss","origin":null,"nation":null,"ethnic":null,"education":null,"introduction":null,"entryTime":null,"pwd":"","title":null,"state":null,"enterpriseId":null,"roles":null,"lastLoginTime":null,"loginIp":null,"loginName":"shixj"}}
     */
    @PutMapping("")
    public CommonResponse modify(@RequestBody Employee employee) {
        try {
            return CommonResponse.buildUpdateSuccessResponse(employeeService.modify(employee));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {PUT} /pwd
     * @apiGroup employee
     * @apiDescription 修改密码
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee/pwd
     * {"id":1,"oldPwd":"123456","newPwd":"1"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改成功！","data":null}
     * {"status":"400","msg":"请确认原密码！","data":null}
     * {"status":"400","msg":"请输入原密码！","data":null}
     * {"status":"400","msg":"请求异常！","data":null}
     */
    @PutMapping("/pwd")
    public CommonResponse modify(@RequestBody PwdChanger pwdChanger) {
        return employeeService.modifyPwd(pwdChanger);
    }

    /**
     * @api {DELETE} /{id}
     * @apiGroup employee
     * @apiDescription 删除雇员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employee/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}}
     */
    @DeleteMapping("/{id}")
    public CommonResponse delById(@PathVariable Long id) {
        employeeService.del(id);
        return CommonResponse.buildDelSuccessResponse("");
    }

    /**
     * 删除合作企业员工
     * @param id
     * @return
     */
    @DeleteMapping("/hz/{id}")
    public CommonResponse delHzById(@PathVariable Long id) {
        employeeService.delHzById(id);
        return CommonResponse.buildDelSuccessResponse("");
    }

}
